package com.sise.bl.dao.impl;

import com.sise.bl.dao.UserDao;
import com.sise.bl.entity.User;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository(value = "userDao")
@SuppressWarnings("all")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {
    @Override
    public Long countUser() {
        String hql = "select count(*) from User";
        return count(hql);
    }

    @Override
    public User  selectUserByUname(String username) {
        String hql = "from User u where u.username = ?0";
        Query query = this.getCurrentSession().createQuery(hql);
        query.setParameter(0, username);
        return (User) query.uniqueResult();
    }

    @Override
    public User selectUserById(Integer uid) {
        return get(uid);
    }

    @Override
    public int insertUser(User user) {
        int i = (int) save(user);
        return i;
    }

    @Override
    public User selectUserByCode(String code) {
        String hql = "from User u where u.code = ?0";
        Query query = this.getCurrentSession().createQuery(hql);
        query.setParameter(0, code);
        return (User) query.uniqueResult();
    }

    @Override
    public int updateUser(User user) {
        try {
            update(user);
            return 1;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<User> selectAll() {
        String sql = "from User";
       List<User> users= (List<User>) find(sql);
        return users;
    }
}
